﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class GameModels
    {
        public List<Game> GetGames()
        {
            List<Game> listgame = new List<Game>();
            ConnectDB con=new ConnectDB();
            try
            {
                string sql = "select GameID,GameName from TblGame";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Game game = new Game();
                    game.GameID =int.Parse(con.dataRead["GameID"].ToString());
                    game.GameName = con.dataRead["GameName"].ToString();
                    listgame.Add(game);
                }
                    
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
            return listgame;
        }
        public void CreateGame(Game game)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_CreateGame @GameName=N'"+game.GameName+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public void EditGame(Game game)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_EditGame @GameID='"+game.GameID+"'"+",@GameName=N'"+game.GameName+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public void DeleteGame(int GameID)
        {

            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_DeleteGame @GameID='"+GameID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public List<Game> GetGameID(int GameID)
        {
            ConnectDB con = new ConnectDB();
            List<Game> listgameid = new List<Game>();
            try
            {
                string sql = "sp_GetGameID @GameID='"+GameID+"'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Game game = new Game();
                    game.GameID = int.Parse(con.dataRead["GameID"].ToString());
                    game.GameName = con.dataRead["GameName"].ToString();
                    listgameid.Add(game);
                }

            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
            return listgameid;
        }
    }
}